package com.atgion.service.service.impl;

import com.atgion.service.mapper.UserInfoMapper;
import com.atgion.service.service.UserInfoService;
import com.atgion.exception.exception.GlobalException;
import com.atgion.exception.exception.GlobalExceptionEnum;

import com.atgion.spzx.model.dto.h5.UserRegisterDto;
import com.atgion.spzx.model.entity.system.SysUser;

import com.atgion.spzx.model.vo.base.ResultCode;
import com.atgion.util.AuthContextUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class UserInfoServiceImpl  extends ServiceImpl<UserInfoMapper, SysUser> implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public void register(UserRegisterDto userRegisterDto) {
        String UserName = redisTemplate.opsForValue().get(userRegisterDto.getUsername());
//        验证验证码
        if(!UserName.equals(userRegisterDto.getCode())){
            throw new GlobalException(GlobalExceptionEnum.ERROR_VERIFY_CODE_WRONG,true);
        }
//        验证用户名是否重复
        SysUser userInfo = userInfoMapper.selectByUserName(userRegisterDto.getUsername());
        if(userInfo!=null){
            throw new GlobalException(ResultCode.ERROR_NAME_REPEAT);
        }
//        注册账号
        userInfo = new SysUser();
        userInfo.setUserName(userRegisterDto.getUsername());
        userInfo.setNickName(userRegisterDto.getNickName());
        userInfo.setPassword(DigestUtils.md5DigestAsHex(userRegisterDto.getPassword().getBytes()));
//        userInfo.setPhone(userRegisterDto.getUsername());
//        userInfo.setStatus(1);
        userInfo.setIsDeleted(0);
        userInfo.setUserImages("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        save(userInfo);
        // 删除Redis中的短信验证码
        redisTemplate.delete(userRegisterDto.getUsername());
    }

    @Override
    public SysUser getCurrentUserInfo() {
        SysUser userInfo = AuthContextUtil.getUserInfo();
        return userInfo;
    }
}
